package ru.paswd.marketprice.ui.activity

import android.Manifest
import android.app.AlertDialog
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import moxy.MvpAppCompatActivity
import ru.paswd.marketprice.R
import ru.paswd.marketprice.ui.view.BaseView

abstract class BaseActivity : MvpAppCompatActivity(), BaseView, View.OnClickListener {

    companion object {
        const val PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1
        const val PERMISSIONS_REQUEST_ACCESS_CAMERA = 2

        val permissionMessages = mapOf(
            PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION
                    to R.string.notification_permission_denied_location,
            PERMISSIONS_REQUEST_ACCESS_CAMERA
                    to R.string.notification_permission_denied_camera
        )
    }

    @JvmField
    var layout = 0

    abstract fun initInterface()

    abstract fun setAttr()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setAttr()
        initView()
        initInterface()
    }

    protected open fun initView() {
        setContentView(layout)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        if (id == android.R.id.home) {
            onBackPressed()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onClick(view: View?) {
    }

    override fun showMessage(message: String?) {
        if (message == null) return
        Toast.makeText(applicationContext, message, Toast.LENGTH_SHORT).show()
    }

    override fun showMessage(resourceId: Int) {
        val str = resources.getString(resourceId)
        showMessage(str)
    }

    override fun showNotificationDialog(title: String, message: String, onDialogClosed: Runnable) {
        val builder = AlertDialog.Builder(this)
        builder.setTitle(title)
            .setMessage(message)
            .setCancelable(false)
            .setPositiveButton("Понятно"
            ) { dialog, _ ->
                dialog.cancel()
                onDialogClosed.run()
            }

        val alertDialog = builder.create()
        alertDialog.show()
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if (requestCode != getPermissionRequestCode()) {
            return
        }

        for (r in grantResults) {
            if (r != PackageManager.PERMISSION_GRANTED) {
                val permissionMessageResource = permissionMessages[getPermissionRequestCode()]

                if (permissionMessageResource != null) {
                    showMessage(permissionMessageResource)
                }

                finish()
                return
            }
        }

        onPermissionsGranted()
    }

    protected fun gettingLocationPermitted(): Boolean {
        return ActivityCompat.checkSelfPermission(
                this, Manifest.permission.ACCESS_FINE_LOCATION) ==
                PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
                this, Manifest.permission.ACCESS_COARSE_LOCATION) ==
                PackageManager.PERMISSION_GRANTED
    }

    protected fun getPermission(permission: String) {
        getPermission(permission, getPermissionRequestCode())
    }

    protected fun getPermission(permission: String, requestCode: Int) {
        if (ContextCompat.checkSelfPermission(this.applicationContext,
                permission) !=
            PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                arrayOf(permission),
                requestCode
            )
        } else {
            onPermissionsGranted()
        }
    }

    protected open fun onPermissionsGranted() {
    }

    protected open fun getPermissionRequestCode() = 0
}